/*
 * Copyright (c) 2013-2018 上海汇数数据. All rights reserved.
 * @(#) I18n.java 2018-08-06 12:37
 */

package cn.jh.common.core.i18n;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Locale;


/**
 * @author Fuchun
 * @since 1.0
 */
public class I18n {

    private static Locale defaultLocale = Locale.getDefault();
    private static I18nMessage i18nMessage;

    public static synchronized void setI18nMessage(I18nMessage i18nMessage) {
        I18n.i18nMessage = i18nMessage;
    }

    @NotNull
    @Contract("_ -> new")
    public static Object[] tryFind(String what) {
        String actual = I18n.getOrDefault(what, null);
        if (actual == null) {
            actual = what;
        }
        return new Object[] { actual };
    }

    public static String getOrDefault(String code, Object[] args, Locale locale, String defaultMessage) {
        return i18nMessage.getOrDefault(code, args, locale, defaultMessage);
    }

    public static String getOrDefault(String code, Locale locale, String defaultMessage) {
        return i18nMessage.getOrDefault(code, null, locale, defaultMessage);
    }

    public static String getOrDefault(String code, @Nullable Object[] args, String defaultMessage) {
        return i18nMessage.getOrDefault(code, args, defaultLocale, defaultMessage);
    }

    public static String getOrDefault(String code, @Nullable String defaultMessage) {
        return i18nMessage.getOrDefault(code, (Object[]) null, defaultMessage);
    }

    public static String get(String code, Object[] args, Locale locale) {
        return i18nMessage.get(code, args, locale);
    }

    public static String get(String code, Object[] args) {
        return i18nMessage.get(code, args);
    }

    public static String get(String code, Locale locale) {
        return get(code, null, locale);
    }

    public static String get(String code) {
        return get(code, null, defaultLocale);
    }
}
